/***************************************************************************
 *   Copyleft 2006-2007 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __BSPLINESURFACE_H__
#define __BSPLINESURFACE_H__

#include "./Mesh.h"
#include "../Curves/BasicCurve.h"
#include "../Curves/BSplineCurve.h"

typedef DynamicArray<BasicCurve*> BSplineCurveList;


class BSplineSurface: public Mesh {
private:
	BSplineCurveList mSurfaceCurves;
	cBool mSurfacesVisible;

	/**************************************************************
	* Trimming fields			                                  *
	***************************************************************/
	DynamicArray<Vector2D> mHole;
	cBool mIsHole;
	cBool mIsHoleInterior;

	cVoid CalculateSurface();
	cVoid CalculateN(cFloat t, cFloat *N, cInt n = 3);			// Calculate b-spline basis

	cVoid ClearMeshLines();
	cVoid MakeMeshLines();

	/**************************************************************
	* Trimming private functions                                  *
	***************************************************************/
	//void floodFill(Array2d<bool> &tab);
	//bool isIntersected(float val1, float val2, float horizon, bool isHorizon);
	//float calculateIntersection(float val1, float val2, float horizon, bool isHorizon);
	//bool calculateIntersectionU(float u, Array<float> &list);
	//bool calculateIntersectionV(float v, Array<float> &list);
	//void makeMeshLines(Array2d<bool> &tab);
public:

    /**************************************************************
    * Constructors                                                *
    ***************************************************************/
    BSplineSurface(cInt iU = 7, cInt iV = 7);
    virtual ~BSplineSurface(); 
        
    /**************************************************************
    * Operators                                                   *
    ***************************************************************/


    /**************************************************************
    * Other helpful functions                                     *
    ***************************************************************/
	cVoid CreateSurface(AbstractCurve* curve1, AbstractCurve* curve2);
	virtual cChar* GetName();
	virtual cVoid DisplayInfo(Renderer& renderer);
	virtual cVoid Render(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane);
	virtual cVoid Render(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane, cWinColor color);

	virtual cVoid UpdateMeshPoint(cInt iRow, cInt iCol, Point4D point);
	Point4D CalculatePoint(cFloat u, cFloat v);
	virtual cVoid ChangeU(cInt iU);
	virtual cVoid ChangeV(cInt iV);
	cVoid SwitchSurfaceVisibility();
	virtual cVoid SwitchPeriodic();
	virtual cVoid SetPeriodic(cBool bPeriodic);

	
	/**************************************************************
	* Trimming functions                                          *
	***************************************************************/
	Point4D CalculateUDerivative(cFloat u, cFloat v);
	Point4D CalculateVDerivative(cFloat u, cFloat v);
	Point4D CalculateNormalVersor(cFloat u, cFloat v);

	cVoid SetHole(DynamicArray<Vector2D> &h);
	cVoid DeleteHole();
	cVoid SetIsHoleInterior(cBool v);
	cBool GetIsHoleInterior();

	/**************************************************************
	* Serializing to XML functions                                *
	***************************************************************/
	virtual TiXmlElement* Serialize(TiXmlElement* parent);
};                                                                 

#endif // __BSPLINESURFACE_H__